|
|
|
|
|
|
|
In addition, the Raise method of the Err object can be used to create custom errors for your application. There may be conditions that can occur in your application that you consider errors, that Visual Basic considers legal code processing. For these types of problems, you can create your own error numbers and when they occur, you can Raise them and deal with them as you see fit. You can also use the other properties of the Raise method to set a topic in a help file that the user can view. |
|
|
|
|
|
|
|
|
Handling Data Access Errors |
|
|
|
|
|
|
|
|
Data access errors are more sensitive to the way they can be resolved. There are many more things to worry about when working with a database. As discussed earlier today, with Data Controls, you don't need to code any error routines in the data control's DataErr event. However, for Data Access Objects, you do need to trap any data access errors that might occur. Because any errors that occur during database access have the capability to damage or destroy data, the way you handle a particular error is based on what the error is. One of the best ways of coping with data access errors is by using the built-in transaction processing statements. The transaction processing commands enable you to control the final outcome of the data access process: |
|
|
|
|
|
|
|
|
These commands enable you to reverse a database function to the original data if an error occurs. After a BeginTrans statement is executed, every action that is done to a database can be reversed or rolled back until a CommitTrans statement is executed. Using this concept, you can create code that backs out of an error or even enables the user to cancel changes that she already made. The following code is an example of enabling the user to cancel her actions: |
|
|
|
|
|
|
|
|
Private Sub cmdClose_Click()
If MsgBox(Do you want to save your changes?, vbYesNo) = vbYes Then
MyWorkspace.CommitTrans
Else
MyWorkspace.Rollback
End If
End Sub |
|
|
|
|
|
|
|
|
Note You should be aware that the Transaction processing that you get using CommitTrans and Rollback are not error-handling methods; they are instead a way to inform the database that any changes should either be saved or thrown away. |
|
|
|
|
|
|